﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NTUTMessagerAPI;

namespace NTUTMessager
{
    public partial class CommunicationForm : Form
    {
        SocketAPI _socketAPI;
        public String _account;
        public String _ip;

        public CommunicationForm(SocketAPI socketAPI,String account,String ip)
        {
            _socketAPI = socketAPI;
            _account = account;
            _ip = ip;

            InitializeComponent();


        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void CommunicationForm_Load(object sender, EventArgs e)
        {
            this.Text = "ip:" + _ip;

            if (isConnected())
            {

            }
            else 
            {

                _socketAPI.ConnectTo(_ip);
            }


        }

        public void GetMessage(String message) 
        {
            textBox2.Text += _account + "：" + message + Environment.NewLine;
        }


        private Boolean isConnected() 
        {
            Boolean result = false;

            foreach (ConnectPair pair in _socketAPI.connectedList) 
            {
                if (pair.address.ToString().Contains(_ip)) 
                {
                    return true;
                }
            }

            Console.WriteLine("Connected? = " + result);

            return result;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendMessage();
        }

        private void SendMessage()
        {
            textBox1.Text = textBox1.Text.Trim(Environment.NewLine.ToCharArray());
            _socketAPI.SendMessage(System.Net.IPAddress.Parse(_ip), textBox1.Text);
            textBox2.Text += "我：" + textBox1.Text + Environment.NewLine;
            
            textBox1.Clear();
        }

        public void GetCommand(String str) 
        {
            textBox2.Text += str + Environment.NewLine;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            //VerticalScroll.

            textBox2.SelectionStart = textBox2.Text.Length;
            textBox2.ScrollToCaret();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (!e.Shift && e.KeyCode == Keys.Enter)
            {
                SendMessage();
                e.Handled = true;
            }
        }

        public OpenFileDialog openDialog = new OpenFileDialog();

        private void button2_Click(object sender, EventArgs e)
        {
            openDialog.Filter = "All files (*.*)|*.*";
            openDialog.FilterIndex = 1;
            openDialog.RestoreDirectory = true;
            openDialog.Multiselect = false;

            if (openDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                GetCommand("等待對方接收:『" + openDialog.FileName+"』.......");
                _socketAPI.SendCommand(System.Net.IPAddress.Parse(_ip),"FileTable = { [\"FileName\"] = \"" + System.IO.Path.GetFileName(openDialog.FileName) + "\" }");
            }
        }


    }
}
